let ajax = getApp().globalData.ajax;
let app = getApp();
let starUrl = app.globalData.fileUrl + 'd974f42cf782aac0d88da7313fe3af04_26_26.jpeg';
let blackStarUrl = app.globalData.fileUrl + '4f1e2758b6896f0ebb23768b7be5875c_26_26.jpeg'
let innerAudioContext = ''
Page({
  /**
   * 页面的初始数据
   */
  data: {
    anchorDetail: {},
    isMyself: false,
    audioIcon: 'http://image.tanwancat.com/aa6af4c843fc05169491106ca46c8053_20_20.jpeg',
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let user_skill_id = options.user_skill_id;
    ajax('skill/person/detail', { user_skill_id }, ).then(res => {
      let anchorDetail = res.data;
      let currentChatTo = anchorDetail.user.im_token.accId
      if (currentChatTo === app.globalData.loginUser['account']) {
        this.setData({
          isMyself: true
        })
      }
      anchorDetail.userSkillId = user_skill_id;
      let heatNumber = Number(anchorDetail.heat),
        heatList = [];
      for (let i = 1; i <= 5; i++) {
        if (i <= heatNumber) {
          heatList.push(starUrl);
        } else {
          heatList.push(blackStarUrl);
        }
      }
      anchorDetail.heatList = heatList;
      this.setData({
        anchorDetail
      })
    })
  },
  chatBtnHandler (e) {
    let account = e.currentTarget.dataset.account
    wx.navigateTo({
      url: '../../partials/chating/chating?chatTo=' + account,
    })
  },
  toOrder (e) {
    let anchorDetail = this.data.anchorDetail;
    let skill = JSON.stringify(anchorDetail.skill);
    wx.navigateTo({
      url: `../toOrder/toOrder?skill=${skill}&price=${anchorDetail.price}&sellerId=${anchorDetail.user.user_id}&avatar=${anchorDetail.user.avatar}&nick=${anchorDetail.user.nick}&userSkillId=${anchorDetail.userSkillId}`,
    })
  },

  audioPlay (e) {
    let src = e.currentTarget.dataset.src
    if (innerAudioContext.src === src) {
      if (innerAudioContext.paused) {
        innerAudioContext.play()
      } else {
        innerAudioContext.pause()
      }
    } else {
      innerAudioContext.src = src
    }
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function (e) {
    // 使用 wx.createAudioContext 获取 audio 上下文 context
    innerAudioContext = wx.createInnerAudioContext()
    innerAudioContext.autoplay = true
    innerAudioContext.onPlay(() => {
      console.log('开始播放')
    })
    innerAudioContext.onPause(() => {
      console.log('暂停播放')
    })
    innerAudioContext.onError((res) => {
      console.log(res);
    })
  },
  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
    innerAudioContext.destroy()
  },
  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
    innerAudioContext.destroy()
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})